package com.aj.http.response;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.aj.databases.entry.Family;

public class FamilyRelativeResponse extends BaseRespond {
	private List<Family> publicFamily = new ArrayList<Family>();
	private List<Family> privateFamily = new ArrayList<Family>();

	public FamilyRelativeResponse(JSONObject json) {
		super(json);
		try {
			JSONObject resultJSONObject = common.getResult();
			if (resultJSONObject != null) {
				if (!resultJSONObject.isNull("list")) {
					JSONArray jsonArray = resultJSONObject.getJSONArray("list");
					Family family = null;
					JSONObject tempJSONJsonObject = null;
					for (int i = 0; i < jsonArray.length(); i++) {
						tempJSONJsonObject = jsonArray.getJSONObject(i);
						family = new Family();
						if (tempJSONJsonObject.has("userId")) {
							family.setUserid(tempJSONJsonObject
									.optString("userId"));
						}
						if (tempJSONJsonObject.has("callId")) {
							family.setCallId(tempJSONJsonObject
									.optString("callId"));
						}
						if (tempJSONJsonObject.has("callName")) {
							family.setCallName(tempJSONJsonObject
									.optString("callName"));
						}
						if (tempJSONJsonObject.has("photo")) {
							family.setPhoto_url(tempJSONJsonObject
									.optString("photo"));
						}
						if (tempJSONJsonObject.has("isMarried")) {
							family.setIsMarried(tempJSONJsonObject
									.optInt("isMarried"));
						}
						if (tempJSONJsonObject.has("age")) {
							family.setAge(tempJSONJsonObject.optInt("age"));
						}
						family.setIsPrivate(1);
						publicFamily.add(family);
					}
				}
				if (!resultJSONObject.isNull("privateUsers")) {
					JSONArray jsonArray = resultJSONObject
							.getJSONArray("privateUsers");
					Family family = null;
					JSONObject tempJSONJsonObject = null;
					for (int i = 0; i < jsonArray.length(); i++) {
						tempJSONJsonObject = jsonArray.getJSONObject(i);
						family = new Family();
						if (tempJSONJsonObject.has("userId")) {
							family.setUserid(tempJSONJsonObject
									.optString("userId"));
						}
						if (tempJSONJsonObject.has("callId")) {
							family.setCallId(tempJSONJsonObject
									.optString("callId"));
						}
						if (tempJSONJsonObject.has("callName")) {
							family.setCallName(tempJSONJsonObject
									.optString("callName"));
						}
						if (tempJSONJsonObject.has("photo")) {
							family.setPhoto_url(tempJSONJsonObject
									.optString("photo"));
						}
						if (tempJSONJsonObject.has("isMarried")) {
							family.setIsMarried(tempJSONJsonObject
									.optInt("isMarried"));
						}
						if (tempJSONJsonObject.has("age")) {
							family.setAge(tempJSONJsonObject.optInt("age"));
						}
						family.setIsPrivate(0);
						privateFamily.add(family);
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public List<Family> getPublicFamily() {
		return publicFamily;
	}

	public void setPublicFamily(List<Family> publicFamily) {
		this.publicFamily = publicFamily;
	}

	public List<Family> getPrivateFamily() {
		return privateFamily;
	}

	public void setPrivateFamily(List<Family> privateFamily) {
		this.privateFamily = privateFamily;
	}

}
